package bsts.view.customer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import bsts.application.BSTSApplication;
import bsts.model.Account;
import bsts.model.Interest;

import bsts.util.validation.NRICValidation;

public class AddCustomerPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JFrame container;
	private BSTSApplication manager;

	private JPanel pnlMain;
	private JPanel pnlBasicInfo;
	private JPanel pnlCustomer;
	private JPanel pnlInterest;
	private JPanel pnlInterestInfo;
	private JPanel pnlInterestBtns;
	private JPanel pnlInterestDisp;
	private JPanel pnlButtons;

	private JLabel lblSurname;
	private JLabel lblFirstname;
	private JLabel lblSecondname;
	private JLabel lblNric;
	private JLabel lblAddress;
	private JLabel lblTelephoneNo;
	private JLabel lblSalutation;
	private JLabel lblInterest;
	private JLabel lblDescription;

	private JTextField txtSurname;
	private JTextField txtFirstname;
	private JTextField txtSecondname;
	private JTextField txtNric;
	private JTextField txtAddress;
	private JTextField txtTelephoneNo;
	private JComboBox cbxSalutation;
	private JTextField txtInterest;
	private JTextField txtDescription;

	private JTable tblInterests;
	private JScrollPane scrInterests;

	private JButton btnAddInterest;
	private JButton btnDelInterest;
	private JButton btnSave;
	private JButton btnCancel;

	private ActionListener actlis_btnAddInterest;
	private ActionListener actlis_btnDelInterest;
	private ActionListener actlis_btnSave;
	private ActionListener actlis_btnCancel;

	private NRICValidation nv = new NRICValidation();

	private ArrayList<Interest> interests = new ArrayList<Interest>();

	public AddCustomerPanel(JFrame container, BSTSApplication manager) {
		this.container = container;
		this.manager = manager;

		pnlMain = new JPanel();
		pnlBasicInfo = new JPanel();
		pnlCustomer = new JPanel();
		pnlInterest = new JPanel();
		pnlInterestInfo = new JPanel();
		pnlInterestBtns = new JPanel();
		pnlInterestDisp = new JPanel();
		pnlButtons = new JPanel();

		pnlMain.setLayout(new BorderLayout());
		pnlCustomer.setLayout(new GridLayout(7, 2));
		pnlInterest.setLayout(new BoxLayout(pnlInterest, BoxLayout.Y_AXIS));
		pnlInterestInfo.setLayout(new GridLayout(3, 2));
		pnlInterestBtns.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pnlInterestDisp.setLayout(new BorderLayout());
		pnlButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));

		pnlBasicInfo.setBorder(BorderFactory.createTitledBorder("Basic Info"));
		pnlInterest.setBorder(BorderFactory.createTitledBorder("Interest"));

		lblSurname = new JLabel("Surname:");
		lblFirstname = new JLabel("First name:");
		lblSecondname = new JLabel("Second name:");
		lblNric = new JLabel("NRIC:");
		lblAddress = new JLabel("Address:");
		lblTelephoneNo = new JLabel("Telephone No.:");
		lblSalutation = new JLabel("Salutation:");
		lblInterest = new JLabel("Interest:");
		lblDescription = new JLabel("Remark:");

		txtSurname = new JTextField();
		txtFirstname = new JTextField();
		txtSecondname = new JTextField();
		txtNric = new JTextField();
		txtAddress = new JTextField();
		txtTelephoneNo = new JTextField();
		cbxSalutation = new JComboBox();
		txtInterest = new JTextField();
		txtDescription = new JTextField();

		txtSurname.setPreferredSize(new Dimension(150, 20));

		cbxSalutation.addItem(new String("Dr"));
		cbxSalutation.addItem(new String("Mr"));
		cbxSalutation.addItem(new String("Mrs"));
		cbxSalutation.addItem(new String("Miss"));
		cbxSalutation.addItem(new String("Mdm"));

		tblInterests = new JTable();
		scrInterests = new JScrollPane();

		initTbl();
		scrInterests
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrInterests.add(tblInterests, null);
		scrInterests.setViewportView(tblInterests);
		scrInterests.setPreferredSize(new Dimension(500, 150));

		btnAddInterest = new JButton("Add");
		btnDelInterest = new JButton("Remove");
		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");

		pnlCustomer.add(lblSurname);
		pnlCustomer.add(txtSurname);
		pnlCustomer.add(lblFirstname);
		pnlCustomer.add(txtFirstname);
		pnlCustomer.add(lblSecondname);
		pnlCustomer.add(txtSecondname);
		pnlCustomer.add(lblNric);
		pnlCustomer.add(txtNric);
		pnlCustomer.add(lblAddress);
		pnlCustomer.add(txtAddress);
		pnlCustomer.add(lblTelephoneNo);
		pnlCustomer.add(txtTelephoneNo);
		pnlCustomer.add(lblSalutation);
		pnlCustomer.add(cbxSalutation);

		pnlBasicInfo.add(pnlCustomer);

		pnlInterestInfo.add(lblInterest);
		pnlInterestInfo.add(txtInterest);
		pnlInterestInfo.add(lblDescription);
		pnlInterestInfo.add(txtDescription);

		pnlInterestBtns.add(btnAddInterest);
		pnlInterestBtns.add(btnDelInterest);

		pnlInterestDisp.add(scrInterests, BorderLayout.CENTER);

		pnlInterest.add(pnlInterestInfo);
		pnlInterest.add(pnlInterestBtns);
		pnlInterest.add(pnlInterestDisp);

		pnlButtons.add(btnSave);
		pnlButtons.add(btnCancel);

		pnlMain.add(pnlBasicInfo, BorderLayout.WEST);
		pnlMain.add(pnlInterest, BorderLayout.EAST);
		pnlMain.add(pnlButtons, BorderLayout.SOUTH);

		this.add(pnlMain);

		initActionListener();

		// Testing
		txtNric.setInputVerifier(nv);
		txtNric.addActionListener(nv);
		// Testing
	}

	public void refreshTbl() {
		tblInterests.revalidate();
		tblInterests.repaint();
	}

	private void initTbl() {
		final String[] strHeaded = { "Interest", "Remark" };

		// Create a model of the data.
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			public int getColumnCount() {
				return strHeaded.length;
			}

			public int getRowCount() {
				return interests.size();
			}

			public Object getValueAt(int row, int col) {
				Interest interest = interests.get(row);

				if (col == 0) {
					return interest.getName();
				} else if (col == 1) {
					return interest.getDescription();
				} else {
					return null;
				}
			}

			public String getColumnName(int column) {
				return strHeaded[column];
			}

			public Class<?> getColumnClass(int col) {
				return getValueAt(0, col).getClass();
			}
		};

		tblInterests = new JTable(dataModel);
		tblInterests.getSelectionModel();
		tblInterests.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableCellRenderer right = new DefaultTableCellRenderer();
		right.setHorizontalAlignment(DefaultTableCellRenderer.TRAILING);

		tblInterests.setDefaultRenderer(Object.class, right);

		int[] intWidth = { 200, 300 };

		for (int i = 0; i < intWidth.length; i++) {
			TableColumn tblCol = tblInterests.getColumn(strHeaded[i]);
			tblCol.setMaxWidth(intWidth[i]);
		}
	}

	private void initActionListener() {
		actlis_btnAddInterest = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtInterest.getText().trim().length() > 0) {
					Interest interest = new Interest(txtInterest.getText()
							.trim(), txtDescription.getText().trim());
					interests.add(interest);

					refreshTbl();
				} else {
					JOptionPane.showMessageDialog(container,
							"Please enter the information for interest.");
				}
			}
		};

		actlis_btnDelInterest = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (tblInterests.getSelectedRow() >= 0) {
					interests.remove(tblInterests.getSelectedRow());
					refreshTbl();
				} else {
					JOptionPane.showMessageDialog(container,
							"Please select a record.");
				}
			}
		};

		actlis_btnSave = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String strNric = txtNric.getText().trim();

				if (strNric.length() > 0
						&& !manager.getCustomerManager().isCustomerExists(
								strNric)) {
					Account account = manager.getCustomerManager()
							.registerCustomerWithDefAcc(
									txtSurname.getText().trim(),
									txtFirstname.getText().trim(),
									txtSecondname.getText().trim(), strNric,
									txtAddress.getText().trim(),
									txtTelephoneNo.getText().trim(),
									cbxSalutation.getSelectedItem().toString(),
									interests, manager.getLoginUser());
					JOptionPane.showMessageDialog(
							container,
							"New account with ID: "
									+ ((account != null) ? account
											.getAccountNo() : "")
									+ " is created.");
					manager.getViewManager().goBackMain(container);
				} else {
					if (strNric.length() <= 0) {
						JOptionPane.showMessageDialog(container,
								"Please fill up a valid NRIC.");
					} else {
						JOptionPane.showMessageDialog(container,
								"Duplicate NRIC is found.");
					}
				}
			}
		};

		actlis_btnCancel = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getViewManager().goBackMain(container);
			}
		};

		btnAddInterest.addActionListener(actlis_btnAddInterest);
		btnDelInterest.addActionListener(actlis_btnDelInterest);
		btnSave.addActionListener(actlis_btnSave);
		btnCancel.addActionListener(actlis_btnCancel);
	}
}
